#coding=utf-8
from django.db import models
from django.contrib.auth.models import User

class Group(models.Model):
    name = models.CharField(max_length=150)
    slug = models.SlugField(unique=True)

    description = models.TextField(blank=True)
    
    def __unicode__(self):
        return self.name
    
class Thread(models.Model):
    group = models.ForeignKey(Group)
   
    author = models.ForeignKey(User)
    created_at = models.DateTimeField(auto_now_add=True)
    
    title = models.CharField(max_length=250)
    
    is_closed = models.BooleanField(default=False)
    closed_at = models.DateTimeField(blank=True, null=True)
    
    hits = models.PositiveIntegerField(default=1)
    post_count = models.PositiveIntegerField(default=1)
    
    last_active = models.DateTimeField()
    
    def __unicode__(self):
        return self.title
        
    @models.permalink
    def get_absolute_url(self):
        return ('forum.views.details_thread', [self.group.slug, str(self.id)])

    def get_posts(self):
        return self.post_set.filter(is_deleted=False).order_by('created_at').all()
    posts = property(fget=get_posts)
    
class Post(models.Model):

    author = models.ForeignKey(User)
    title = models.CharField(max_length=250)
    content = models.TextField(blank=True)
    
    created_at = models.DateTimeField(auto_now_add=True)
    modified_at = models.DateTimeField(auto_now=True)
    
    is_deleted = models.BooleanField(default=False)
    deleted_at = models.DateTimeField(blank=True, null=True)
    
    thread = models.ForeignKey(Thread)
    
    def __unicode__(self):
        return self.title
